package com.kaiwukj.android.ufamily.mvp.ui.page.mine.house;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseMvpActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.di.component.DaggerMineComponent;
import com.kaiwukj.android.ufamily.di.module.MineModule;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.DoorAccessDeviceResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.DoorAccessRecordResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.MyHouseMemberResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.MyHouseResult;
import com.kaiwukj.android.ufamily.mvp.ui.page.mine.MineContract;
import com.kaiwukj.android.ufamily.mvp.ui.widget.SpaceItemDecoration;
import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUrlKt.MyHouseUrl)
public class MyHouseActivity extends BaseMvpActivity<MyHousePresenter> implements MineContract.HouseView {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.empty_view)
    QMUIEmptyView emptyView;

    private MyHouseAdapter mListAdapter;

    @Autowired
    boolean isPick = false;

    @OnClick(R.id.container_left)
    void onBackClick() {
        onBackPressedSupport();
    }

    @Override
    protected int getLayoutId() {
        ARouter.getInstance().inject(this);
        return R.layout.activity_list_normal;
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        super.setupActivityComponent(appComponent);
        DaggerMineComponent.builder()
                .appComponent(appComponent)
                .mineModule(new MineModule(this))
                .build().inject(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvTitle.setText("我的房屋");
        mRefreshLayout.setEnableLoadMore(false);
        rvList.setLayoutManager(new LinearLayoutManager(this));
        rvList.addItemDecoration(new SpaceItemDecoration(15));
        mListAdapter = new MyHouseAdapter(isPick);
        mListAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (isPick) {
                Intent intent = new Intent();
                intent.putExtra("EXTRA_KEY_HOUSE_ID", mListAdapter.getItem(position).getRoomId());
                intent.putExtra("EXTRA_KEY_HOUSE_NAME", mListAdapter.getItem(position).getGroupAddress());
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        rvList.setAdapter(mListAdapter);
        mRefreshLayout.setOnRefreshListener(refreshLayout -> mPresenter.fetchHouseList());

    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mPresenter.fetchHouseList();
        emptyView.show(true);
    }


    @Override
    public void onTaskErr(int taskId) {
        super.onTaskErr(taskId);
        emptyView.hide();
        mRefreshLayout.finishRefresh(false);
    }

    @Override
    public void onFetchHouse(List<MyHouseResult> results) {
        emptyView.hide();
        mRefreshLayout.finishRefresh(true);
        mListAdapter.setList(results);
        if (mListAdapter.getItemCount() == 0) {
            emptyView.show("您在该小区暂没有房屋", "");
        }
    }

    @Override
    public void onFetchHouseMember(List<MyHouseMemberResult> results) {

    }

    @Override
    public void onFetchDoorAccess(DoorAccessDeviceResult result) {

    }

    @Override
    public void onFetchDoorRecord(List<DoorAccessRecordResult> results) {

    }
}
